package com.workflow.service.impl;

import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.StringToken;
import com.workflow.bean.Exitcard;
import com.workflow.bean.Exitdeptdetail;
import com.workflow.service.ExitcardService;

public class ExitcardServiceImpl extends AbstractManager implements
		ExitcardService {

	public void addExitcard(String cardno, String exitdetailno) {
		Exitcard exitcard = new Exitcard();
		Cardinfo cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Exitdeptdetail exitdeptdetail = (Exitdeptdetail) this.getHibernateTemplate().load(Exitdeptdetail.class, exitdetailno);
		exitcard.setCardno(cardinfo.getCardno());
		exitcard.setExitdeptdetail(exitdeptdetail);
		
		Datadict datadictByStatus = new Datadict();
		//置卡状态为锁定状态  75l
		datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
		cardinfo.setDatadictByStatus(datadictByStatus);
		cardinfo.setBelongto(exitdetailno);
		
		this.getHibernateTemplate().update(cardinfo);
		this.getHibernateTemplate().saveOrUpdate(exitcard);
	}

	@SuppressWarnings("unchecked")
	public List<Exitcard> findAllExitcard() {
		String hql = "from Exitcard ec order by ec.exitdeptdetail.exitdetailno	,ec.cardno ";
		return this.getHibernateTemplate().find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Exitcard> findAllExitcardByExitdetailno(String exitdetailno) {
		String hql = "from Exitcard ec where ec.exitdeptdetail.exitdetailno = ? order by ec.cardno ";
		return this.getHibernateTemplate().find(hql);
	}

	public Exitcard findExitcard(String cardno) {
		return (Exitcard) this.getHibernateTemplate().load(Exitcard.class,  cardno);
	}

	public void removeExitcard(String cardno) {
		Exitcard exitcard = (Exitcard) this.getHibernateTemplate().load(Exitcard.class,  cardno);
		if(exitcard != null) {
			this.getHibernateTemplate().delete(exitcard);
			
			Cardinfo cardinfo = null;
			cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
			Datadict datadictByStatus = new Datadict();
//			置卡状态为库存 状态编号 76l
			datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_STORE);
			cardinfo.setDatadictByStatus(datadictByStatus);
			cardinfo.setBelongto("");
			this.getHibernateTemplate().update(cardinfo);
		}
	}

	public void updateExitcard(String cardno, String exitdetailno) {
		Exitcard exitcard = new Exitcard();
		Cardinfo cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Exitdeptdetail exitdeptdetail = (Exitdeptdetail) this.getHibernateTemplate().load(Exitdeptdetail.class, exitdetailno);
		exitcard.setCardno(cardinfo.getCardno());
		exitcard.setExitdeptdetail(exitdeptdetail);
		
		Datadict datadictByStatus = new Datadict();
		//置卡状态为锁定状态  75l
		datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
		cardinfo.setDatadictByStatus(datadictByStatus);
		cardinfo.setBelongto(exitdetailno);
		
		this.getHibernateTemplate().update(cardinfo);
		this.getHibernateTemplate().saveOrUpdate(exitcard);
	}

}
